package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author MrZhao
 * @created 2025-10-07 18:13
 * @description 用户管理Controller
 *
 */
@Slf4j
@RestController
@RequestMapping(path = "/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    @GetMapping
    public Result list(UserDTO dto) {
        PageResult<UserDO> pageResult = userService.list(dto);
        log.info("分页查询用户结果为：{}",pageResult);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable List<Integer> ids){
        log.info("批量删除用户id为：{}",ids);
        userService.deleteUser(ids);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result getUserById(@PathVariable Integer id){
        log.info("查询用户id为：{}",id);
        User user =userService.getUserById(id);
        return Result.success(user);
    }
    @PutMapping
    public Result updateUser(@RequestBody User user){
        log.info("修改用户信息为：{}",user);
        userService.updateUser(user);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有用户");
        return Result.success(userService.selectAll());
    }
    @GetMapping("/role/{roleLabel}")
    public Result selectRole(@PathVariable String roleLabel){
        log.info("查询角色为：{}",roleLabel);
        return Result.success(userService.selectRoleLabel(roleLabel));
    }

    @GetMapping("dept/{deptId}")
    public Result selectDept(@PathVariable Integer deptId){
        log.info("查询部门为：{}",deptId);
        return Result.success(userService.selectDept(deptId));
    }
}
